package com.jcli.jb51_net.article._271803_Pong.com.mr.view;

import com.jcli.jb51_net.article._271803_Pong.com.mr.model.Ball;
import com.jcli.jb51_net.article._271803_Pong.com.mr.model.Board;
import com.jcli.jb51_net.article._271803_Pong.com.mr.service.Fresh;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * TODO 2.主面板
 *
 * 整个程序的核心，在这里我们要做出对赛点、得分、球体碰撞等检测，并绘制图形
 *
 * @author li
 * @version 1.0
 * @belongsProject novel_20230923
 * @belongsPackage com.jcli.jb51_net.article._271803_Pong.com.mr.view
 * @className GamePanel
 * @createTime 2024-02-24  08:39
 */
public class GamePanel extends JPanel implements KeyListener {
    private BufferedImage img;
    private Graphics2D g2;
    public ArrayList<Object> select;
    private Fresh fresh;
    private Board b1;
    private Board b2;
    private Ball ball;
    private int score1;
    private int score2;
    private int winPoint;
    /**
     * 0表示没有赛点，1表示玩家1的赛点，2表示玩家2的赛点
     */
    private int matchPoint;
    private int winner;

    public GamePanel() {
        img = new BufferedImage(850, 1000, BufferedImage.TYPE_INT_BGR);
        g2 = img.createGraphics();
        select = new ArrayList<>();
        b1 = new Board(0);
        b2 = new Board(1);
        ball = new Ball();
        score1 = 0;
        score2 = 0;
        matchPoint = 0;
        winPoint = 11;
        winner = 0;
        fresh = new Fresh(this);
        fresh.start();
    }

    private void paintImage() {
        g2.setColor(Color.WHITE);
        if (winner == 0) {
            int width = 10;
            g2.fillRect(425 - width / 2, 0, width, 1000);
            g2.fillRect(b1.x, b1.y, b1.width, b1.height);
            g2.fillRect(b2.x, b2.y, b2.width, b2.height);
            g2.fillOval(ball.x - ball.r, ball.y - ball.r, ball.r * 2, ball.r * 2);
        }
        g2.setFont(new Font("黑体", Font.BOLD, 56));
        g2.drawString(String.valueOf(score1), 295, 150);
        g2.drawString(String.valueOf(score2), 525, 150);
        g2.setFont(new Font("黑体", Font.PLAIN, 22));
        if (winner == 0) {
            switch (matchPoint) {
                case 1:
                    g2.drawString("赛点", 295, 200);
                    break;
                case 2:
                    g2.drawString("赛点", 525, 200);
                    break;
                default:
                    break;
            }
        } else {
            g2.drawString("玩家" + String.valueOf(winner) + "获胜", winner == 1 ? 295 : 525, 200);
        }
    }

    public void paint(Graphics g) {
        if (winner == 0) {
            move();
            ball.move(b1.getBound(), b2.getBound());
            checkPoint();
            checkMatchPoint();
            b1.checkBound();
            b2.checkBound();
        }
        g2.setColor(Color.BLACK);
        g2.fillRect(0, 0, 850, 1000);
        paintImage();
        g.drawImage(img, 0, 0, this);
    }

    private void checkPoint() {
        if (ball.x <= 0) {
            score2 += 1;
        } else if (ball.x + ball.r * 2 >= 850) {
            score1 += 1;
        } else {
            return;
        }
        ball = new Ball();
    }

    private void checkMatchPoint() {
        if (score1 == winPoint) {
            winner = 1;
            return;
        } else if (score2 == winPoint) {
            winner = 2;
            return;
        }
        if (score1 + 1 == winPoint && score2 + 1 != winPoint) {
            matchPoint = 1;
        } else if (score1 + 1 != winPoint && score2 + 1 == winPoint) {
            matchPoint = 2;
        } else if (score1 + 1 == winPoint && score2 + 1 == winPoint) {
            matchPoint = 0;
            winPoint++;
        } else if (score1 + 1 != winPoint && score2 + 1 != winPoint) {
            matchPoint = 0;
        }
    }

    private void move() {
        for (Object code : select) {
            if (code == (Object) KeyEvent.VK_W) {
                b1.y -= b1.speed;
            } else if (code == (Object) KeyEvent.VK_S) {
                b1.y += b1.speed;
            } else if (code == (Object) KeyEvent.VK_UP) {
                b2.y -= b2.speed;
            } else if (code == (Object) KeyEvent.VK_DOWN) {
                b2.y += b2.speed;
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent event) {
        if (select.indexOf(event.getKeyCode()) == -1) {
            select.add(event.getKeyCode());
        }
    }

    @Override
    public void keyReleased(KeyEvent event) {
        select.remove((Object) event.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent event) {

    }
}
